package com.inspinia.upms.server.controller;

import com.inspinia.base.cache.RedisUtil;
import com.inspinia.base.common.model.UserCacheKeys;
import com.inspinia.upms.common.model.user.UpmsUser;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by Veblen on 2017/1/1.
 */
public abstract class BaseController {

    /**
     * 日志对象
     */
    protected Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 获取登陆用户的id
     *
     * @return
     */
    protected Long getPrincipalId() {
        UpmsUser principal = (UpmsUser) SecurityUtils.getSubject().getPrincipal();
        return principal.getUserId();
    }

    /**
     * 获取登陆用户的账户
     *
     * @return
     */
    protected String getPrincipalName() {
        UpmsUser principal = (UpmsUser) SecurityUtils.getSubject().getPrincipal();
        return principal.getUserName();
    }

    /**
     * 查询当前用户真实姓名
     *
     * @return
     */
    protected String getPrincipalRealName() {
        UpmsUser principal = (UpmsUser) SecurityUtils.getSubject().getPrincipal();
        return principal.getRealName();
    }

    /**
     * 清除用户已有权限的缓存
     */
    protected void cleanUserResourceCache() {
        UpmsUser principal = (UpmsUser)SecurityUtils.getSubject().getPrincipal();
        RedisUtil.delete(UserCacheKeys.getUserMenuKey(principal.getUserId()));
    }
}
